<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>R'Agro Harvest Predictor</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-image: url('{{ background_image }}');
            background-size: cover;
            background-repeat: no-repeat;
            background-attachment: fixed;
            margin: 0;
            padding: 0;
        }

        header {
            background-color: #4CAF50;
            color: #fff;
            text-align: center;
            padding: 20px;
        }
        h1 {
            font-size: 36px;
        }

        .container {
            max-width: 700px;
            margin: 20px auto;
            background-color: rgba(255, 255, 255, 0.8);
            border-radius: 5px;
            box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2);
            padding: 20px;
        }
        label {
            display: block;
            margin-bottom: 10px;
            font-weight: bold;
        }

        .input-container {
            margin-bottom: 10px;
        }

        input[type="text"] {
            width: 95%;
            padding: 12px;
            border: 2px solid #4CAF50;
            border-radius: 5px;
            font-size: 16px;
        }
        button {
            background-color: #4CAF50;
            color: #fff;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
        }

        #result {
            font-weight: bold;
            margin-top: 20px;
            font-size: 18px;
        }
    </style>
</head>
<body>
    <header>
        <h1>R'Agro Harvest Predictor</h1>
    </header>
    <div class="container">
        <form id="prediction-form" action="{{ url_for('predict') }}" method="post">
            <div class="input-container">
                <label for="temperature">Temperature (°C)</label>
                <input type="text" id="temperature" name="temperature" required>
            </div>

            <div class="input-container">
                <label for="rainfall">Rainfall (mm)</label>
                <input type="text" id="rainfall" name="rainfall" required>
            </div>

            <div class="input-container">
                <label for="fertilizer">Fertilizer Amount (kg)</label>
                <input type="text" id="fertilizer" name="fertilizer" required>
            </div>

            <div class="input-container">
                <label for="land-area">Land Area (acres)</label>
                <input type="text" id="land-area" name="land-area" required>
            </div>

            <button type="submit">Predict Harvest</button>
        </form>

        <div id="result">
            {{ prediction_text }}
        </div>
    </div>

    <script>

        function validateInput(inputElement) {
            const inputValue = inputElement.value;

            const validInput = /^-?\d*(\.\d*)?$/.test(inputValue);

            if (!validInput) {
                inputElement.value = '';
            }
        }


        const inputElements = document.querySelectorAll('input[type="text"]');
        inputElements.forEach(function (inputElement) {
            inputElement.addEventListener('input', function () {
                validateInput(inputElement);
            });
        });

        document.getElementById('prediction-form').addEventListener('submit', function (e) {
            e.preventDefault();

            const temperatureInput = document.getElementById('temperature').value;
            const rainfallInput = document.getElementById('rainfall').value;
            const fertilizerInput = document.getElementById('fertilizer').value;
            const landAreaInput = document.getElementById('land-area').value;

            const temperature = parseFloat(temperatureInput);
            const rainfall = parseFloat(rainfallInput);
            const fertilizer = parseFloat(fertilizerInput);
            const landArea = parseFloat(landAreaInput);

            if (!isNaN(temperature) && !isNaN(rainfall) && !isNaN(fertilizer) && !isNaN(landArea)) {
                const predictedHarvest = 1000 + (2 * temperature) + (1.5 * rainfall) + (0.5 * fertilizer) - (0.2 * landArea);

                document.getElementById('result').innerHTML = `Predicted Harvest: ${predictedHarvest.toFixed(2)} kg`;
            } else {
                document.getElementById('result').innerHTML = 'Please enter valid numeric values.';
            }
        });
    </script>
</body>
</html>
